﻿using GalaSoft.MvvmLight;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SoftHelper.Model;
using SoftHelper.Tool;
using System;

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace SoftHelper.ViewModel
{
    public class DispositionViewModel : ViewModelBase
    {
        private ObservableCollection<TagContainerItemModel> languageTypeslist = new ObservableCollection<TagContainerItemModel>();

        public ObservableCollection<TagContainerItemModel> LanguageTypesList
        {
            get { return languageTypeslist; }
            set { languageTypeslist = value; RaisePropertyChanged("LanguageTypes"); }
        }

        /// <summary>
        /// 添加规则
        /// </summary>
        public RalyCommand AddItemCmd
        {
            get
            {
                return new RalyCommand(async (o) =>
                {

                    TextBox textBox = o as TextBox;
                    if (textBox.Text.Trim()!=String.Empty)
                    {
                        LanguageTypesList.Add(new TagContainerItemModel() { Name = textBox.Text, IsSelected = true, });
                        await SaveNeedLanguageTypes();
                        textBox.Text = String.Empty;
                        //刷新数据刷新主页面的数据
                        AnalysisProjectViewModel.RefreshMethod();
                    }
                }, x => true);
            }
        }

        /// <summary>
        /// 当规则被修改后进行保存
        /// </summary>
        public RalyCommand SaveChange
        {
            get
            {
                return new RalyCommand(async(o) =>
                {
                   await SaveNeedLanguageTypes();
                    //刷新数据刷新主页面的数据
                    AnalysisProjectViewModel.RefreshMethod();
                },x=>true);
            }
        }

        public async Task SaveNeedLanguageTypes()
        {
            LanguageTypesConfigModel languageTypesConfigModel = new LanguageTypesConfigModel();
            foreach (TagContainerItemModel item in LanguageTypesList)
            {
                languageTypesConfigModel.LanguageTypes.Add(item);
            }
            String json = JsonConvert.SerializeObject(languageTypesConfigModel);
            await IOTool.ToWrite(System.Windows.Forms.Application.StartupPath + "languageTypesConfig.json", json);
        
        }

        public DispositionViewModel()
        {
            init();
        }

        private async void init()
        {

            String json = await IOTool.ToRead(System.Windows.Forms.Application.StartupPath + "/languageTypesConfig.json");
            JObject jobject = JObject.Parse(json);
            IList<JToken> jTokens = jobject["LanguageTypes"].Children().ToList();
            foreach (JToken jToken in jTokens)
            {
                TagContainerItemModel model = jToken.ToObject<TagContainerItemModel>();
                LanguageTypesList.Add(model);
            }

        }
    }
}
